<?php
/**
 * Author: wutong
 */
class Db {
	private static $instance;
	public $dbHost = "localhost";
	public $dbName = "mics1";
	public $dbUser = "root";
	public $dbPwd = "sa";
	private $dbConn;
	private $result;
	private $sql;
	private $sql_error;
	
	private function __construct() {
	}
	public function open($dbName=null){
		if($dbName!=null){
			$this->dbName=$dbName;
		}
		//$dbName = $dbName == null ? $this->dbName : $dbName;
		if (($this->dbConn = @mysql_connect ( $this->dbHost, $this->dbUser, $this->dbPwd,false,131072 )) === false) {
			exit( "Don't connection to MySQL Server.<br>" .mysql_error());
		} else {
			if (mysql_select_db ( $this->dbName, $this->dbConn ) === false) {
				exit( "Don't select DataBase \"{$this->dbName}\".<br>" .mysql_error());
			}
		}		
	}
	public function query($sql){
		return mysql_query($sql,$this->dbConn);	
	}
	private function setLog($content){
		return;
		$date=date("Y-m-d");
		$dir="./Public/log";
		if(!file_exists($dir)){
			mkdir($dir);
		}
		$log=$date.".log";
		$fp=fopen($dir."/".$log,"a+b");
		$content=date("Y-m-d H:i:s")."\r".$content."\n\r";
		fwrite($fp,$content);
		fclose($fp);
	}
	/**
	 * Description 获取实例
	 */
	public static function getInstance() {
		if (self::$instance == null) {
			self::$instance = new Db ();
		}
		return self::$instance;
	}
	/**
	 * Description 设置查询编码
	 * @param $charset string 编码类型 
	 */
	public function setCharset($charset = "utf8") {
		mysql_query ( "SET NAMES {$charset}" );
	}
	/**
	 * 
	 * Description 查询
	 * @param string $sql 要查询的SQL语句
	 * @param string $charset 查询之前要设置的编码类型
	 */
	public function select($sql, $charset = "utf8") {
		$this->setCharset ( $charset );
		$this->sql = $sql;
		if (($this->result = mysql_query ( $sql, $this->dbConn )) === false) {
			$this->sql_error = mysql_error ( $this->dbConn );
			$this->setLog($this->getSqlError());
			return false;
		} else {
			return $this->result;
		}
	}
	/**
	 * 
	 * Description 插入
	 * @param string $sql 要插入的SQL语句
	 * @param string $charset 插入之前要设置的编码类型
	 */
	public function insert($sql, $charset = "utf8") {
		$this->setCharset ( $charset );
		$this->sql = $sql;
		if (($this->result = mysql_query ( $sql, $this->dbConn )) === false) {
			$this->sql_error = mysql_error ( $this->dbConn );
			$this->setLog($this->getSqlError());
			return false;
		} else {
			return mysql_insert_id($this->dbConn);
		}
	}
	/**
	 * 
	 * Description 获取集合中的数据.
	 * @param resource $result 查询之后获得的结果集
	 * @param string $charset 编码类型
	 */
	public function getRs($result = null, $charset = "utf8") {
		$this->setCharset ( $charset );
		$result = $result == null ? $this->result : $result;
		if (($rows = mysql_fetch_assoc ( $result )) === false) {
			$this->sql_error = mysql_error ( $this->dbConn );
			$this->setLog($this->getSqlError());
			return false;
		} else {
			return $rows;
		}
	}
	/**
	 * 
	 * Description 更新数据.
	 * @param string $sql 更新的SQL语句
	 * @param string $charset 编码类型
	 */
	public function update($sql, $charset = "utf8") {
		$this->setCharset ( $charset );
		$this->sql = $sql;
		if (($this->result = mysql_query ( $sql )) === false) {
			$this->sql_error = mysql_error ( $this->dbConn );
			$this->setLog($this->getSqlError());
			return false;
		} else {
			return mysql_affected_rows ( $this->dbConn );
		}
	}
	/**
	 * 
	 * Description 删除数据.
	 * @param string $sql 更新的SQL语句
	 * @param string $charset 编码类型
	 */
	public function delete($sql, $charset = "utf8") {
		$this->setCharset ( $charset );
		$this->sql = $sql;
		if (($this->result = mysql_query ( $sql )) === false) {
			$this->sql_error = mysql_error ( $this->dbConn );
			$this->setLog($this->getSqlError());
			return false;
		} else {
			return mysql_affected_rows ( $this->dbConn );
		}
	}
	/**
	 * 
	 * Description 获取查询的SQL
	 * @return string
	 */
	public function getSQL() {
		return $this->sql;
	}
	/**
	 * 
	 * Description 获取查询SQL出错的信息
	 * @return string
	 */
	public function getSqlError() {
		return $this->sql_error;
	}
    /**
     * Description 将所有结果以一个数组返回
     * @param #res $result
     * @param string $charset
     * @return array
     */
	public function getArray($result = null, $charset = "utf8"){
		$arr=array();
		$this->setCharset ( $charset );
        $result = $result == null ? $this->result : $result;
        while($rs=mysql_fetch_assoc($result)){
            array_push($arr,$rs);
        }
		return $arr;
	}
    /**
     * Description 获取结果集中的行数
     * @param #res $result
     * @return int
     */
    public function getRows($result = null){
        $result = $result == null ? $this->result : $result;
        return mysql_num_rows($result);
    }
	//获取字段长度
	public function getFieldsLength($result = null){
		$result = $result == null ? $this->result : $result;
		return mysql_num_fields($result);	
	}
	//获取字段名称
	public function getField($index=0,$result = null){
		$result = $result == null ? $this->result : $result;
		return mysql_field_name($result,$index);	
	}
	public function __toString(){
		return "数据库连接类";	
	}
	public function close(){
		mysql_close($this->dbConn);	
	}
}